﻿using System.Web;
using RaceList.activity.util;

namespace RaceList.activity.help
{
    /// <summary>
    ///     这个类不需要设计成satic,当前对static的理解比较肤浅
    ///     其实他不是在缓存数据，而是直接在取session中的数据而已
    ///     这里封装起来只是为了方便在Session中存储一个对象而已（UserData,UserChengji）
    /// </summary>
    public static class SessionHelp
    {
        private static UserData _userData;
        private static UserChengji _userChengji;
        private static int _cycleId;
        private static int? _sponsorId;

        public static string IndexUrl
        {
            get { return "Relayindex.aspx?" + Parameters.cycle + "=" + CycleId; }
        }

        public static int CycleId
        {
            get { return _cycleId; }
            set
            {
                _cycleId = value;
                HttpContext.Current.Session[Keys.CycleId] = _cycleId;
            }
        }

        public static int? SponsorId
        {
            get { return _sponsorId; }
            set
            {
                _sponsorId = value;
                HttpContext.Current.Session[Keys.SponsorId] = _sponsorId;
            }
        }

        public static UserData UserData
        {
            get
            {
                _userData = HttpContext.Current.Session[Keys.UserDatastr] as UserData;
                if (_userData == null)
                {
                    _userData = new UserData();
                }
                return _userData;
            }
            set
            {
                _userData = value;
                HttpContext.Current.Session[Keys.UserDatastr] = _userData;
            }
        }

        public static UserChengji UserChengji
        {
            get
            {
                _userChengji = HttpContext.Current.Session[Keys.UserChengjistr] as UserChengji;
                if (_userChengji == null)
                {
                    _userChengji = new UserChengji();
                }
                return _userChengji;
            }
            set
            {
                _userChengji = value;
                HttpContext.Current.Session[Keys.UserChengjistr] = _userChengji;
            }
        }

        /// <summary>
        ///     Session zhong clean
        /// </summary>
        //public static void CleanUserChengji()
        //{
        //    HttpContext.Current.Session[Keys.UserChengjistr] = null;
        //}

        //public static void CleanUserData()
        //{
        //    HttpContext.Current.Session[Keys.UserDatastr] = null;
        //}

        /// <summary>
        ///     通过用户是否登陆
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool ValidUserData()
        {
            UserData data = UserData;
            if (data != null)
            {
                if (data.WeixinUserId != 0)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        ///     是否是被用户点击过了，clciknum>0
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool ValidHasClciked_ByUserChengji()
        {
            UserData data = UserData;
            if (data != null)
            {
                //只要正常点击的话，ClickNum>0,IsClicked=true
                if (UserChengji.ClickNum != 0 && UserChengji.IsClicked)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        ///     如果记录成绩的Session不存在则返回true
        ///     如果存在则判断clicknum和flag
        /// </summary>
        /// <param name="session"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool IsFirstClick()
        {
            UserChengji data = UserChengji;
            if (data == null) //一开始应该是这个样子的，就是null，然后插入默认值
            {
                return true;
            }
            if (data != null)
            {
                //如果没有点击过就是ClickNum=0，IsClicked=false;
                if (data.ClickNum == 0 && !data.IsClicked)
                {
                    return true;
                }
            }
            return false;
        }
    }
}